package com.jrx.anytxn.accounting.job.voucherFile.config;

import com.jrx.anytxn.accounting.config.AnytxnBatchProperties;
import com.jrx.anytxn.accounting.entity.GaVoucherInfo;
import com.jrx.anytxn.accounting.job.voucherFile.step.VoucherFileListener;
import com.jrx.anytxn.accounting.job.voucherFile.step.VoucherFileProcessor;
import com.jrx.anytxn.accounting.job.voucherFile.step.VoucherFileReader;
import com.jrx.anytxn.accounting.job.voucherFile.step.VoucherFileWriter;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.Date;


/**
 * 生成传票文件
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class VoucherFileConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    private VoucherFileProcessor voucherFileProcessor;

    @Autowired
    private VoucherFileWriter voucherFileWriter;

    @Autowired
    private VoucherFileListener voucherFileListener;

    @Bean
    public Job voucherFileJob() {
        return jobBuilderFactory.get("voucherFileJob")
                .start(voucherFileStep()).incrementer(new RunIdIncrementer()).listener(voucherFileListener)
                .build();
    }

    @Bean
    public Step voucherFileStep() {
        return stepBuilderFactory.get("voucherFileStep")
                .<GaVoucherInfo, String>chunk(anytxnBatchProperties.getVoucherFileConfiguration().getChunkLimit())
                .reader(voucherFileReader())
                .processor(voucherFileProcessor)
                .writer(voucherFileWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getVoucherFileConfiguration().getThrottleLimit())//多线程*/
                .build();

    }

    @Bean
    @StepScope
    public ItemReader<GaVoucherInfo> voucherFileReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        Date businessDate = org.getToday();
        return new VoucherFileReader(businessDate,sqlSessionFactory, anytxnBatchProperties.getGlInterfaceConfig().getPageSize());
    }
}
